/**
 * @version Create on 2012-11-9
 * @author Yinzi Chen
 */

public class NextPermutation {

	public void nextPermutation(int[] num) {
		int n = num.length;
		int i, j;
		for (i = n - 2; i >= 0; --i) {
			if (num[i] < num[i + 1])
				break;
		}
		int tmp;
		for (j = i + 1; j <= (n + i) / 2; ++j) {
			tmp = num[j];
			num[j] = num[n - j + i];
			num[n - j + i] = tmp;
		}
		if (i == -1)
			return;
		for (j = i + 1; j < n; ++j) {
			if (num[i] < num[j])
				break;
		}
		tmp = num[i];
		num[i] = num[j];
		num[j] = tmp;
	}

	public static void main(String[] args) {
		NextPermutation a = new NextPermutation();
		a.nextPermutation(new int[] { 1, 3, 2 });
	}

}
